import MarkdownIt from "markdown-it";

// 代码高亮解析
import Prism from "prismjs";
import "prismjs/components/prism-clike"
import "prismjs/components/prism-kotlin"
import "prismjs/components/prism-java"
import "prismjs/components/prism-javascript"
import "prismjs/components/prism-sass"
import "prismjs/components/prism-less"
import "prismjs/components/prism-cshtml"
import "prismjs/components/prism-bash"
import "prismjs/components/prism-python"
import "prismjs/components/prism-yaml"
import "prismjs/components/prism-go"
import "prismjs/components/prism-sql"

// 表情
import emoji from "markdown-it-emoji";
// 下标
import sub from "markdown-it-sub";
// 上标
import sup from "markdown-it-sup";
// <dl/>
import deflist from 'markdown-it-deflist';
// <abbr/>
import abbr from'markdown-it-abbr';
// footnote
import footnote from 'markdown-it-footnote';
// insert 带有下划线 样式 ++ ++
import insert from 'markdown-it-ins';
// mark
import mark from 'markdown-it-mark';
// taskLists
import taskLists from 'markdown-it-task-lists';
// container
import container from 'markdown-it-container';
// math katex
import katex from 'markdown-it-katex-external';

const markdown_config = {
  html: true,
  linkify: true,
  typographer: true,
  highlight: function(str, lang) {
    // 当前时间加随机数生成唯一的id标识
    var d = new Date().getTime();
    const codeIndex = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
      /[xy]/g,
      function(c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
      }
    );
    // 复制功能主要使用的是 clipboard.js
    const linesLength = str.split(/\n/).length - 1;
    // 生成行号
    let linesNum = '<span aria-hidden="true" class="line-numbers-rows">';
    for (let index = 0; index < linesLength; index++) {
      linesNum = linesNum + "<span></span>";
    }
    linesNum += "</span>";

    if (lang) {
      if(Prism.languages[lang] != undefined){
        let html = Prism.highlight(str, Prism.languages[lang]);
        return `<pre class="line-numbers"><code class="language-${lang}">${html}</code>${linesNum}</pre>`;
      }else{
        return `${str}`;
      }
    }
  }
};

var md = new MarkdownIt(markdown_config);
// add target="_blank" to all link
var defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
    return self.renderToken(tokens, idx, options);
};
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
    // If you are sure other plugins can't add `target` - drop check below
    var aIndex = tokens[idx].attrIndex('target');

    if (aIndex < 0) {
        tokens[idx].attrPush(['target', '_blank']); // add new attribute
    } else {
        tokens[idx].attrs[aIndex][1] = '_blank';    // replace value of existing attr
    }

    // pass token to default renderer.
    return defaultRender(tokens, idx, options, env, self);
};

md.use(emoji)
    .use(taskLists)
    .use(sup)
    .use(sub)
    .use(container)
    .use(container, 'hljs-left') /* align left */
    .use(container, 'hljs-center')/* align center */
    .use(container, 'hljs-right')/* align right */
    .use(deflist)
    .use(abbr)
    .use(footnote)
    .use(insert)
    .use(mark)
    .use(container)
    // 数学公式不支持服务器端渲染，暂关闭
    // .use(katex)

export default md